package com.cloudlead.dictionaryAction.service.impl;

import com.cloudlead.commonData.service.RegionService;
import com.cloudlead.commonData.vo.Region;
import com.cloudlead.dictionaryAction.service.RegionEditSer;
import com.cloudlead.dictionaryService.vo.RegionVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by wyj on 2015/8/18.
 */
@Service
public class RegionEditSerImpl implements RegionEditSer {

    @Autowired
    RegionService regionService;//spring 注入

    @Override
    public String createRegion(RegionVo vo) {
        Region re = new Region();
            BeanUtils.copyProperties(vo, re);//对象类型转换vo-re
        regionService.create(re);
        return "success";
    }

    @Override
    //单值查询
    public RegionVo findOneRegion(RegionVo vo) {
        Region re = new Region();
        BeanUtils.copyProperties(vo, re);//对象类型转换vo-re
        re=regionService.findOne(re);//实例查询
        RegionVo rv = new RegionVo();
        BeanUtils.copyProperties(re, rv);//对象类型转换re-rv
        return rv;
    }

    @Override
    //多值查询
    public List<RegionVo> findAllRegion(RegionVo vo) {
        Region re = new Region();
        BeanUtils.copyProperties(vo, re);//对象类型转换vo-re
        List<Region> lr=regionService.list(re, null);//列表查询
        List<RegionVo> lvo = new ArrayList<RegionVo>();
        for(Iterator<Region> it = lr.iterator();it.hasNext();) {//遍历
            RegionVo rv=new RegionVo();
            BeanUtils.copyProperties(it.next(), rv);//类型转换
            lvo.add(rv);
        }
        return lvo;
    }

    @Override
    public String delRegion(String id) {
        regionService.delete(id);
        return "success";
    }
}
